/**
 * 校验方式二 错误信息在input外,也可以通过定位的方式，在input-icon类div中添加：data-fix="-5,80"，前面数字为上下坐标：像素值，后面为左右坐标：%比值，且都能为负数；
 * 如果不添加，则默认为-20，60
 * @param formId
 */
function initPopupValid(formId) {
    var form = $('#' + formId);

    form.validate({
        errorElement: 'span', //default input error message container
        errorClass: 'help-block help-block-error', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: "",  // validate all fields including form hidden input

        WMHandler: function () {
            if ($(".layui-layer-tips").length > 0)
                Metronic.scrollTo($(".layui-layer-tips"), 0);
        },

        errorPlacement: function (error, element) { // render error placement for each input type
            var icon = $(element).parent('.input-icon').children('i');
            icon.removeClass('fa-check').addClass("fa-warning");
            icon.attr("title", error.text());

            var inputxy = $.trim($(element).parent('.input-icon').attr("data-fix"));
            var x = 0;
            var y = 0;
            if (inputxy != "" && inputxy != null) {
                x = parseInt(inputxy.split(",")[0]);
                y = parseInt(inputxy.split(",")[1]);
            }

            if (error.text() != "" && error.text() != null) {
                var st = $(element).parent('.input-icon');
                if (st.find(".layui-layer-tips").length > 0) {
                    st.find(".layui-layer-tips").remove();
                }
                $(element).parent(".input-icon").append(
                    "<div class=\"layui-layer layui-anim layui-layer-tips\" type=\"tips\" contype=\"object\" " +
                    "style=\"position: absolute;top:" + (x == 0 ? "-20" : x) + "px!important;left:" + (y == 0 ? "60" : y) + "%!important;min-width:110px;z-index:3;\"><div class=\"layui-layer-content\">" + error.text() + "<i class=\"layui-layer-TipsG layui-layer-TipsR\"></i>" +
                    "</div><span class=\"layui-layer-setwin\"></span></div>"
                );
            }
        },

        highlight: function (element) { // hightlight error inputs
            $(element)
                .closest('.form-group').removeClass("has-success").addClass('has-error'); // set error class to the control group
        }
        ,

        unhighlight: function (element) { // revert the change done by hightlight

        }
        ,

        success: function (label, element) {
            onSuccess(element);
        }
        ,

        submitHandler: function (form) {
        }
    });
}

function onSuccess(element){
    var icon = $(element).parent('.input-icon').children('i');
    $(element).closest('.form-group').removeClass('has-error').addClass('has-success'); // set success class to the control group
    icon.removeClass("fa-warning").addClass("fa-check");
    var st = $(element).parent('.input-icon');
    if (st.find(".layui-layer-tips").length > 0) {
        st.find(".layui-layer-tips").remove();
    }
}
function restPopupValid(formId){
    var form = $('#' + formId);
    form.find(".has-error").removeClass("has-error");
    form.find(".fa-warning").removeClass("fa-warning");
    form.find(".has-success").removeClass("has-success");
    form.find(".fa-check").removeClass("fa-check");
    form.find(".layui-layer-tips").remove();
}
